********************************************************************************
********************************************************************************
* Title: 		comprehension
* Project:		Stress, Ethnicity, and Prosocial Behavior
* Author:		Moritz Poll (moritz.poll@brown.edu)
* PIs:			Johannes Haushofer, Sara Lowes, Abednego Musau, David Ndetei, 
*				Nathan Nunn, Moritz Poll, Nancy Qian
* Purpose:		Comprehension data analysis
********************************************************************************
********************************************************************************
* This do file creates comprehension statistics

set more off
set trace off
pause on
noisily dis as input "Start: Comprehension data analysis"

use  "$cleandata_dir/HIO_for_analysis_wide.dta", clear

keep ID subjectid subject treatment ts_taskorder cubicle session *incorrect* *questions

ds *incorrect*d // All comprehension dummies
local i = 0
foreach var in `r(varlist)' {
	local label`++i' : variable label `var'
	rename `var' incorrectd`i'
	local aux = substr("`label`i''",16,.)
	local aux = subinstr("`aux'"," test question","",.)
	local aux = subinstr("`aux'","Stage 1","Stage (1)",.)
	local aux = subinstr("`aux'","Stage 2","Stage (2)",.)
	local aux = subinstr("`aux'"," 1",": Q1",.)
	local aux = subinstr("`aux'"," 2",": Q2",.)
	local aux = subinstr("`aux'"," 3",": Q3",.)
	local aux = subinstr("`aux'"," 4",": Q4",.)
	local aux = subinstr("`aux'"," 5",": Q5",.)
	local aux = subinstr("`aux'","Stage (1)","Stage 1",.)
	local aux = subinstr("`aux'","Stage (2)","Stage 2",.)
	local aux = subinstr("`aux'","final","Final",.)
	local aux = subinstr("`aux'","got ","",.)
	local aux = subinstr("`aux'"," wrong. (Dummy)","",.)
	local valuelabels `"`valuelabels' `i' `"`aux'"' "'
}

label define comp_dummy `valuelabels'

ds *test*incorrect* // All numbers of attempts
local i = 0
foreach var in `r(varlist)' {
	local label`++i' : variable label `var'
	rename `var' incorrect`i'
	local aux = subinstr("`aux'"," test question","",.)
	local aux = subinstr("`aux'"," test question","",.)
	local aux = subinstr("`aux'","final","Final",.)
	local aux = subinstr("`aux'","got ","",.)
	local aux = subinstr("`aux'"," wrong.","",.)
	local valuelabels `"`valuelabels' `i' `"`aux'"' "'
}
label define comp_count `valuelabels'
reshape long incorrectd incorrect, i(ID) j(question)
label values question comp_dummy
gen correctd = 1-incorrectd
label var incorrect "Incorrect"
label var correct "Correct"
order correct, after(incorrect)
gen game = 1
replace game = 2 if (question > 4 & question < 9)   | question == 20
replace game = 3 if (question > 8 & question < 14)  | question == 21 | question == 22
replace game = 4 if (question > 13 & question < 18) | question == 23
gen order = question 
replace order =  4.1 if question == 18
replace order =  4.2 if question == 19
replace order =  8.1 if question == 20
replace order = 13.1 if question == 21
replace order = 13.2 if question == 22
replace order = 17.1 if question == 23

graph hbar incorrectd correctd, stack percentage ///
	over(question, sort(order) gap(10) label(labsize(small))) ysize(14) xsize(20) ///
	bar(1, color(maroon)) bar(2, color(green)) graphregion(color(white)) ///
	legend(off) ytitle("") ylabel(0 (10) 100)

graph export "$figures_dir/comprehension_byquestion.pdf", replace

foreach game in dg cyd tg1 tg2 total {
	dis "${`game'_name}:"
	ttest `game'_incorrect_answers, by(ts_taskorder)
}

hist total_incorrect_answers, discrete width(`=1/22') $hist_design ytitle("Share of sample") xtitle("Share of incorrect answers (on first attempt)") xlabel(0(0.2)1)
graph export "$figures_dir/comprehension_byparticipant.pdf", replace

noisily dis as input "End: Comprehension data analysis"
